import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import localCache from '@/utils/cache'
import { firstMenu } from '@/utils/map-menus'
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/main'
  },
  {
    path: '/login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "home" */ '../views/login/login.vue')
  },
  {
    path: '/main',
    name: 'Main',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/main/main.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'notFound',
    component: () => import('../views/notFound/notFound.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})
//判断当前如果非login页面判断没有token跳转到login
router.beforeEach((to) => {
  if (to.path !== '/login') {
    console.log(to.path, 'topath')
    const token = localCache.getCache('token')
    if (!token) {
      console.log('!token')

      return '/login'
    }
    console.log(router.getRoutes())
    console.log(to)
    if (to.path === '/main') {
      return firstMenu.url
    }
    // if (to.path.indexOf('/main') !== -1) {
    //   if (to.name === 'notFound') {
    //     console.log(firstMenu.url, 'first')
    //     to.path = firstMenu.url
    //     return firstMenu.url
    //   }
    // }
  }
})

export default router
